<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="收货人" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入收货人"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="联系电话" prop="phone">
        <el-input
          v-model="queryParams.phone"
          placeholder="请输入联系电话"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <!-- <el-form-item label="支付状态" prop="ispay">
        <el-select v-model="queryParams.ispay" placeholder="请选择支付状态" clearable>
          <el-option
            v-for="dict in dict.type.sys_pay_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item> -->
      <el-form-item label="关联订单" prop="oid">
        <el-input
          v-model="queryParams.oid"
          placeholder="请输入关联订单"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="所属用户" prop="uid">
        <el-input
          v-model="queryParams.uid"
          placeholder="请输入所属用户"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="打印状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择打印状态" clearable>
          <el-option
            v-for="dict in dict.type.sys_dayin_order"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="物流单号" prop="wuliuInfo">
        <el-input
          v-model="queryParams.wuliuInfo"
          placeholder="请输入物流单号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['ziliao:dayinorder:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['ziliao:dayinorder:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['ziliao:dayinorder:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['ziliao:dayinorder:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="dayinorderList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="ID" align="center" prop="id" />
      <el-table-column label="收货人" align="center" prop="name" />
      <el-table-column label="打印文件" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-view"
            @click="handleViewFile(scope.row)"
            v-hasPermi="['ziliao:dayinorder:edit']"
          >查看</el-button>
        </template>
      </el-table-column>
      <!-- <el-table-column label="打印文件" align="center" prop="filespath" /> -->
      <el-table-column label="收获地址" align="center" prop="addr" />
      <el-table-column label="联系电话" align="center" prop="phone" />
      <!-- <el-table-column label="支付状态" align="center" prop="ispay">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_pay_status" :value="scope.row.ispay"/>
        </template>
      </el-table-column> -->
      <el-table-column label="关联订单" align="center" prop="oid" />
      <el-table-column label="所属用户" align="center" prop="uid" />
      <el-table-column label="打印状态" align="center" prop="status">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_dayin_order" :value="scope.row.status"/>
        </template>
      </el-table-column>
      <el-table-column label="物流单号" align="center" prop="wuliuInfo" />
      <el-table-column label="创建日期" align="center" prop="addTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.addTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="发货日期" align="center" prop="fahuoTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.fahuoTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            v-if="scope.row.status==2"
            size="mini"
            type="text"
            icon="el-icon-check"
            @click="jieshou(scope.row)"
            v-hasPermi="['ziliao:dayinorder:edit']"
          >接收订单</el-button>
          <el-button
            v-if="scope.row.status==3"
            size="mini"
            type="text"
            icon="el-icon-s-promotion"
            @click="fahuo(scope.row)"
            v-hasPermi="['ziliao:dayinorder:edit']"
          >去发货</el-button>
          <el-button
            v-if="scope.row.status==4"
            size="mini"
            type="text"
            icon="el-icon-menu"
            @click="detail(scope.row)"
            v-hasPermi="['ziliao:dayinorder:edit']"
          >详情</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改打印订单对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="收货人" prop="name">
          <el-input v-model="form.name" placeholder="请输入收货人" />
        </el-form-item>
        <el-form-item label="打印文件" prop="filespath">
          <el-input v-model="form.filespath" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="收获地址" prop="addr">
          <el-input v-model="form.addr" placeholder="请输入收获地址" />
        </el-form-item>
        <el-form-item label="联系电话" prop="phone">
          <el-input v-model="form.phone" placeholder="请输入联系电话" />
        </el-form-item>
        <el-form-item label="支付状态" prop="ispay">
          <el-radio-group v-model="form.ispay">
            <el-radio
              v-for="dict in dict.type.sys_pay_status"
              :key="dict.value"
              :label="dict.value"
            >{{dict.label}}</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="关联订单" prop="oid">
          <el-input v-model="form.oid" placeholder="请输入关联订单" />
        </el-form-item>
        <el-form-item label="所属用户" prop="uid">
          <el-input v-model="form.uid" placeholder="请输入所属用户" />
        </el-form-item>
        <el-form-item label="打印状态" prop="status">
          <el-radio-group v-model="form.status">
            <el-radio
              v-for="dict in dict.type.sys_dayin_order"
              :key="dict.value"
              :label="dict.value"
            >{{dict.label}}</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="物流单号" prop="wuliuInfo">
          <el-input v-model="form.wuliuInfo" placeholder="请输入物流单号" />
        </el-form-item>
        <el-form-item label="创建日期" prop="addTime">
          <el-date-picker clearable
            v-model="form.addTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择创建日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="发货日期" prop="fahuoTime">
          <el-date-picker clearable
            v-model="form.fahuoTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择发货日期">
          </el-date-picker>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>


    <!-- 查看文件对话框 -->
    <el-dialog title="文件查看" :close-on-click-modal="false" :destroy-on-close="true" :visible.sync="viewfiles" width="80%" append-to-body>
      <el-table :data="filesList">
        <el-table-column label="文件名称" align="center" prop="name" />
        <el-table-column label="打印份数" align="center" prop="fs" />
        <el-table-column label="支付金额" align="center" prop="jine" />
        <el-table-column label="文件页数" align="center" prop="yeshu" />
        <el-table-column label="纸张类型" align="center" prop="geshi.zhizhang" />
        <el-table-column label="版面形式" align="center" prop="geshi.banmian" />
        <el-table-column label="单双面" align="center" prop="geshi.danshuang" />
        <el-table-column label="色彩类型" align="center" prop="geshi.leixing" />
        <el-table-column label="装订方式" align="center" prop="geshi.zhuangding" />
        <el-table-column label="打印文件" align="center" class-name="small-padding fixed-width">
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="text"
              icon="el-icon-download"
              @click="downloadFiles(scope.row.filePath,scope.row.name)"
            >下载</el-button>
          </template>
        </el-table-column>
      </el-table>
      <div slot="footer" class="dialog-footer">
        <el-button @click="closeTc">关 闭</el-button>
      </div>
    </el-dialog>


  </div>
</template>

<script>
import { listDayinorder, getDayinorder, delDayinorder, addDayinorder, updateDayinorder } from "@/api/ziliao/dayinorder";
export default {
  name: "Dayinorder",
  dicts: ['sys_dayin_order', 'sys_pay_status'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 打印订单表格数据
      dayinorderList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      viewfiles:false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        name: null,
        filespath: null,
        addr: null,
        phone: null,
        ispay: null,
        oid: null,
        uid: null,
        status: 2,
        wuliuInfo: null,
        addTime: null,
        fahuoTime: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      },
      filesList:[]
    };
  },
  created() {
    this.getList();
  },
  methods: {
    downloadFiles(url,name){
      const src = `${url}?t=${new Date().getTime()}`
  fetch(src).then(res => {
    res.blob().then(myBlob => {
      const href = URL.createObjectURL(myBlob)
      const a = document.createElement('a')
      a.href = href
      a.download = name
      a.click()
      a.remove()
    })
  })
    },
    closeTc(){
      this.filesList = []
      this.viewfiles = false
    },
    /** 查询打印订单列表 */
    getList() {
      this.loading = true;
      listDayinorder(this.queryParams).then(response => {
        this.dayinorderList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        name: null,
        filespath: null,
        addr: null,
        phone: null,
        ispay: null,
        oid: null,
        uid: null,
        status: null,
        wuliuInfo: null,
        addTime: null,
        fahuoTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加打印订单";
    },
    handleViewFile(row){
      let files = row.filespath
      let filesJson = JSON.parse(files)
      for(var i=0;i<filesJson.length;i++){
        let geshi = filesJson[i].geshi
        let geshiJson = JSON.parse(geshi)
        filesJson[i].geshi = geshiJson
      }
      this.filesList = filesJson
      this.viewfiles = true
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getDayinorder(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改打印订单";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateDayinorder(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addDayinorder(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除打印订单编号为"' + ids + '"的数据项？').then(function() {
        return delDayinorder(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('ziliao/dayinorder/export', {
        ...this.queryParams
      }, `dayinorder_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
